package com.sc.adminspringboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sc.adminspringboot.pojo.Games;
import com.sc.adminspringboot.service.IGamesService;
import com.sc.adminspringboot.utils.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * <p>
 * 公共游戏库基础信息表 前端控制器
 * </p>
 *
 * @author Carson
 * @since 2025-09-23 11:07:26
 */
@RestController
@RequestMapping("/admin/games")
public class GamesController {
    @Autowired
    private IGamesService gamesService;
    @RequestMapping("/list")
    public ResponseMsg gamesList(@RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "10") Integer size,
                                 @RequestParam(defaultValue = "") String keyword) {

        IPage<Games> pageInfo = new Page<>(page, size);

        // 如果有关键字，则进行条件查询
        if (keyword != null && !keyword.trim().isEmpty()) {
            QueryWrapper<Games> queryWrapper = new QueryWrapper<>();
            // 假设根据游戏名称进行模糊查询，你可以根据实际需求修改字段名
            queryWrapper.like("game_name", keyword);
            pageInfo = gamesService.page(pageInfo, queryWrapper);
        } else {
            // 没有关键字则查询所有
            pageInfo = gamesService.page(pageInfo);
        }

        return ResponseMsg.getInstance(ResponseMsg.SUCCESS, pageInfo);
    }
    @RequestMapping("/add")
    public ResponseMsg gamesAdd(@RequestBody Games games) {

        games.setCreateTime(new Date(System.currentTimeMillis()));
        games.setUpdateTime(new Date(System.currentTimeMillis()));
        System.out.println(games);
        boolean save = gamesService.save(games);
        if (save) {
            return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "添加成功");
        } else {
            return ResponseMsg.getInstance(ResponseMsg.ERROR, "添加失败");
        }
    }
    @RequestMapping("/del")
    public ResponseMsg gamesDel(Integer gameId) {
        boolean remove = gamesService.removeById(gameId);
        if (remove) {
            return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "删除成功");
        } else {
            return ResponseMsg.getInstance(ResponseMsg.ERROR, "删除失败");
        }
    }

}
